//
//  CNPhotoSeletorController.swift
//  PhotoSeletor
//
//  Created by 陈锦聪 on 16/4/3.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import HMImagePicker

private let maxPhotoCount = 9
private let reuseIdentifier = "CNPhotoSeletorCell"

class CNPhotoSeletorController: UICollectionViewController {
    
    private let layout = UICollectionViewFlowLayout()
    /// 已选择的图片数组
    var photos: [UIImage] = [UIImage]()
    
    var assets: [PHAsset]?
    /// 当前indexPath
    private var indexPath: NSIndexPath?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init() {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
    }
    private func prepareCollectionView() {
        collectionView!.registerClass(CNPhotoSeletorCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        layout.itemSize = CGSize(width: 80, height: 80)
        collectionView?.backgroundColor = UIColor.whiteColor()
        collectionView?.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    // MARK: - UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photos.count < maxPhotoCount ? photos.count + 1 : maxPhotoCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CNPhotoSeletorCell
        cell.delegate = self
        if indexPath.item < photos.count {
            cell.image = photos[indexPath.item]
        } else {
            cell.resetAddButton()
        }
        return cell
    }

}
// MARK: - CNPhotoSeletorCellDelegate
extension CNPhotoSeletorController: CNPhotoSeletorCellDelegate {
    func photoSeletorCellDidClickAddButton(cell: CNPhotoSeletorCell) {
//        let picker = UIImagePickerController()
        let picker = HMImagePickerController(selectedAssets: assets) // 图片多选
//        picker.delegate = self
        picker.pickerDelegate = self
        picker.maxPickerCount = maxPhotoCount
        indexPath = collectionView?.indexPathForCell(cell)
        presentViewController(picker, animated: true, completion: nil)
    }
    func photoSeletorCellDidClickDeleteButton(cell: CNPhotoSeletorCell) {
        let index = collectionView?.indexPathForCell(cell)?.item
        photos.removeAtIndex(index!)
        assets?.removeAtIndex(index!)
        collectionView?.reloadData()
    }
}
// MARK: - HMImagePickerControllerDelegate
extension CNPhotoSeletorController: HMImagePickerControllerDelegate {
    func imagePickerController(picker: HMImagePickerController, didFinishSelectedImages images: [UIImage], selectedAssets: [PHAsset]?) {
        photos = images
        assets = selectedAssets
        collectionView?.reloadData()
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}
// MARK: - UIImagePickerControllerDelegate
extension CNPhotoSeletorController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        if indexPath?.item == photos.count {
            photos.append(image.scaleImage())
        } else {
            photos[indexPath!.item] = image
        }
        collectionView?.reloadData()
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}